<?php

/**
 * Class PageService
 */
class PageService extends BaseService
{

    private static $self           =   NULL;


    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {


    }

   public function PageShow($total,$page,$page_size,$url)
   {

//       $page=isset($_GET['page'])?intval($_GET['page']):1;
//       $page_size = '2';
       $pagenum=ceil($total/$page_size);
       $page=min($pagenum,$page);
       if($page-1<1){
           $prepage=1;
       }else{
           $prepage=$page-1;;
       }
       if($page+1>=$pagenum){
           $nextpage=$pagenum;
       }else{
           $nextpage=$page+1;
       }


       //$pageset=($page-1)*$page_size;
       $pagenav='';
       //$pagenav.="显示第<font color='red'>".($total?($pageset+1):0)."-".min($pageset+5,$total)."</font>记录 共<b><font color='yellow'>".$total."</font></b>条记录 现在是第 <b><font color='blue'>".$page."</font></b> 页 ";
//       $pagenav.="<p class='p-cer'><span class='gogo'>当前第".$page."页</span> ";
       if($page<=1)
           $pagenav.="<a class='gogo gopage' style=cursor:not-allowed;>首页</a> ";
       else
           $pagenav.="<a class='gogo gopage' onclick=javascript:dopage('page=1','$url?page=1') style=cursor:pointer;>首页</a> ";
       if($prepage)
           $pagenav.="<a class='gogo gopage' onclick=javascript:dopage('page=$prepage','$url?page=$prepage') style=cursor:pointer;>上一页</a> ";
       else
           $pagenav.="<a class='gogo gopage' style=cursor:not-allowed;>上一页</a> ";

       if ($page<6 && $pagenum<=6){
           $start = 1;
           $end = $pagenum;
        }elseif($page<6&& $pagenum>6){
           $start = 1;
           $end = 6;
        }elseif ($page >= 6 && $page+2<$pagenum){
           $start =$page-3;
           echo $start;
           $end = $page+2;
        }elseif ($page >=  $pagenum-5) {
           $start = $pagenum-5;
           $end = $pagenum;
        }

       for ($i = $start; $i <= $end; $i++) {

           if($page ==$i){
               $pagenav .= "<a class='gogo gopage active' onclick=javascript:dopage('page=$i','$url?page=$i') style=cursor:pointer;>$i</a>";
           }else{
               $pagenav .= "<a class='gogo gopage' onclick=javascript:dopage('page=$i','$url?page=$i') style=cursor:pointer;>$i</a>";
           }

       }

       if($nextpage)
           $pagenav.="<a class='gogo gopage' onclick=javascript:dopage('page=$nextpage','$url?page=$nextpage') style=cursor:pointer;>下一页</a> ";
       else
           $pagenav.="<a class='gogo gopage' style=cursor:not-allowed;>下一页</a> ";
       if($pagenum)
           $pagenav.="<a class='gogo gopage' onclick=javascript:dopage('page=$pagenum','$url?page=$pagenum') style=cursor:pointer;>末页</a> ";
       else
           $pagenav.="<a class='gogo' style=cursor:not-allowed;>末页</a> ";
//       $pagenav.="<span class='gogo'>共".$pagenum."页</span></p>";
       if($page>$pagenum){
//           $pagenav ="";
           echo "error:没有此页".$page;
           exit();
       }
       if($pagenum<=1){
           $pagenav ="";
//           echo "error:没有此页".$page;
//           exit();
       }
       return $pagenav;
   }


}



